package com.example.demo.juc.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description:
 * @Author: xiatian
 * @CreateTime: 2021/8/20
 * @Company: kangxu
 */
public class ReentrantLockDemo {

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void testMethod() {
        lock.lock();
        try {
            //通过创建 Condition 对象来使线程 wait，必须先执行 lock.lock 方法获得锁
            condition.await();
            //condition 对象的 signal 方法可以唤醒 wait 线程
            condition.signal();
            for (int i = 0; i < 5; i++) {
                System.out.println("Thread name = " + Thread.currentThread().getName() + " " + i + 1);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }


}
